package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAccount;
import com.zb.entity.ZjAccountRole;
import com.zb.exception.ZbException;
import com.zb.mapper.ZjAccountRoleMapper;
import com.zb.service.ZjAccountRoleService;
import com.zb.service.ZjAccountService;
import com.zb.view.AccountRoleView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 账号角色表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-18
 */
@Service
public class ZjAccountRoleServiceImpl extends ServiceImpl<ZjAccountRoleMapper, ZjAccountRole> implements ZjAccountRoleService {
    @Resource
    private ZjAccountRoleMapper accountRoleMapper;
    @Resource
    private ZjAccountService accountService;

    // 保存系统超管账号角色
    @Override
    public void saveAdminAccountRole(Long custAccId, Long accountId, Long roleId) {
        ZjAccountRole accountRole = new ZjAccountRole();
        accountRole.setId(IdWorker.getId());
        accountRole.setCustAccId(custAccId);
        accountRole.setAccountId(accountId);
        accountRole.setRoleId(roleId);
        accountRole.setAdmin(true);
        this.save(accountRole);
    }

    @Override
    public void save(Long custAccId, Long accountId, Long roleId) {
        if (null == roleId || roleId == 0) {
            return;
        }
        ZjAccountRole accountRole = accountRoleMapper.getByCustAccIdAndAccountId(custAccId, accountId);
        if (null != accountRole) {
            Assert.isTrue(!accountRole.getAdmin(), "该账号为系统超管，不可修改！", null);
            accountRole.setRoleId(roleId);
            this.updateById(accountRole);
            return;
        }
        accountRole = new ZjAccountRole();
        accountRole.setId(IdWorker.getId());
        accountRole.setCustAccId(custAccId);
        accountRole.setAccountId(accountId);
        accountRole.setRoleId(roleId);
        accountRole.setAdmin(false);
        this.save(accountRole);
    }

    // 批量保存账号角色
    @Override
    public void save(Long custAccId, List<Long> accountIds, Long roleId) throws ZbException {
        accountRoleMapper.deleteByRoleId(roleId);
        if (null == accountIds || accountIds.isEmpty() || null == roleId || roleId == 0) {
            return;
        }
        List<Long> accountIdList = accountRoleMapper.getAccountIdByCustAccIdAndAccountIds(custAccId, accountIds);
        if (null != accountIdList && !accountIdList.isEmpty()) {
            List<String> names = accountService.listByIds(accountIdList).stream().map(ZjAccount::getName).collect(Collectors.toList());
            throw new ZbException("人员[" + String.join("、", names) + "]已经分配了角色，不可重复分配！");
        }
        List<ZjAccountRole> accountRoles = new ArrayList<>();
        ZjAccountRole accountRole = null;
        for (Long accountId : accountIds) {
            accountRole = new ZjAccountRole();
            accountRole.setId(IdWorker.getId());
            accountRole.setCustAccId(custAccId);
            accountRole.setAccountId(accountId);
            accountRole.setRoleId(roleId);
            accountRole.setAdmin(false);
            accountRoles.add(accountRole);
        }
        this.saveBatch(accountRoles);
    }

    // 根据账号ID获取角色ID
    @Override
    public Long getRoleIdByAccountId(Long custAccId, Long accountId) {
        return accountRoleMapper.getRoleIdByAccountId(custAccId, accountId);
    }

    // 查询角色信息
    @Override
    public List<AccountRoleView> getRoleByAccountIds(Long custAccId, List<Long> accountIds) {
        if (CollectionUtil.isEmpty(accountIds)) {
            return new ArrayList<>();
        }
        return accountRoleMapper.getRoleByAccountIds(custAccId, accountIds);
    }

    // 根据[商家ID] & [账号ID]获取角色信息
    @Override
    public AccountRoleView getroleByCustAccIdAndAccountId(Long custAccId, Long accountId) {
        List<Long> accountIds = new ArrayList<>();
        accountIds.add(accountId);
        List<AccountRoleView> viewList = getRoleByAccountIds(custAccId, accountIds);
        return CollectionUtil.isEmpty(viewList)? null : viewList.get(0);
    }

    // 根据商家ID与人员ID删除角色关联信息
    @Override
    public void deleteByCustAccIdAndEmpId(Long custAccId, List<Long> empIdList) {
        if (CollectionUtil.isEmpty(empIdList)) {
            return;
        }
        accountRoleMapper.deleteByCustAccIdAndEmpId(custAccId, empIdList);
    }

}
